import pandas as pd
import numpy as np
import os


def folder_name(save_name):
    folder = os.listdir("./")
    file_name_user_id = {}
    for file in folder:
        if ".xlsx" in file and save_name not in file:
            user_id = file.split("_")[-1].strip(".xlsx")
            file_name_user_id[file] = user_id
    return file_name_user_id


def get_file():
    save_name = "集合"  # 这个为要保存的excel名
    folder = folder_name(save_name)
    print(folder)
    columns = [str(i) for i in range(1, 101)]
    columns = "用户id," + ",".join(columns) + "\n"
    with open(save_name + ".csv", "w+") as f:
        f.write(columns)

    for file, user_id in folder.items():
        print("正在处理", user_id, "用户")

        data = pd.read_excel(file)
        music_id = list(data['歌名id'].astype(str))
        music_id.insert(0, user_id)
        user_music = ",".join(music_id)
        with open(save_name + ".csv", "a+") as f:
            f.write(user_music + "\n")


get_file()
